#region Copyright Vanderlande Industries B.V. 2009-2011
//
// Copyright (c) 2009-2011 Vanderlande Industries.
// All rights reserved.
//
// @file:     UserEvent.cs
// @version:  %version: %
// %date_created: %
//
// The copyright to the computer program(s) herein is the property of
// Vanderlande Industries. The program(s) may be used and/or copied
// only with the written permission of the owner or in accordance with
// the terms and conditions stipulated in the contract under which the
// program(s) have been supplied.
#endregion

using System;
using System.Collections.Generic;
using System.Reflection;


namespace VI.Fsc.OperatorGui.Models.Fsc.UserEvents
{
    /// <summary>
    /// This class encapsulates user events originating from the FSC.
    /// </summary>
    public class CUserEvent
    {
        private int uniqueHeaderId;
        private string eventId;
        private string textKey;
        private string data;
        private DateTime timestamp;
        private string componentId;

        public int UniqueHeaderId
        {
            get
            {
                return uniqueHeaderId;
            }
        }
        
        protected string BuildEventDescription()
        {
            string context = "EventDefinitions|";
            string prefix = "EventDefinition.";
            string eventDescription = "";
                //DefaultTextProvider.Instance.GetTextList(context + prefix + this.textKey,
                //VIForm.TextSession.Language, null)[0];

            if (!String.IsNullOrEmpty(data))
            {
                string[] eventDataArray = data.Split('|');

                foreach (string eventDataValue in eventDataArray)
                {
                    int beginIndex = eventDescription.IndexOf('{');
                    int endIndex = eventDescription.IndexOf('}');

                    if (beginIndex >= 0 && endIndex >= 0)
                    {
                        eventDescription = eventDescription.Remove(beginIndex, endIndex - beginIndex + 1);
                        eventDescription = eventDescription.Insert(beginIndex, eventDataValue);
                    }
                }
            }
            //Logger.Debug(MethodBase.GetCurrentMethod(), "Language = " + VIForm.TextSession.Language + ", text key = " +
            //    context + prefix + this.textKey + ", text = " + eventDescription);

            return eventDescription;
        }

        public DateTime Timestamp
        {
            get
            {
                return timestamp;
            }
        }

        public string ComponentId
        {
            get
            {
                return componentId;
            }
        }

        /// <summary>
        /// Creates a new CUserEvent.
        /// </summary>
        /// <param name="headerId"></param>
        /// <param name="eventId">The ID of the user event.</param>
        /// <param name="textKey">The text key of the user event.</param>
        /// <param name="data">Parameter that will be integrated in the text message.</param>
        /// <param name="timestamp">The timestamp of the event.</param>
        /// <param name="componentId"></param>
        public CUserEvent(int headerId, string eventId, string textKey, string data, DateTime timestamp, string componentId)
        {
            this.uniqueHeaderId = headerId;
            this.eventId = eventId;
            this.textKey = textKey;
            this.data = data;
            this.timestamp = timestamp;
            this.componentId = componentId;
        }

        public Dictionary<string, object> ToDataSourceRow()
        {
            Dictionary<string, object> row = new Dictionary<string, object>();

            row.Add("Timestamp", Timestamp);
            row.Add("UniqueHeaderId", UniqueHeaderId);
            row.Add("Id", this.eventId);
            row.Add("Event", BuildEventDescription());
            row.Add("ComponentId", ComponentId);
            row.Add("Updated", true);

            return row;
        }

        override public string ToString()
        {
            string userEventString = 
                " [UserEvent] " +
                " TimeStamp=" + Timestamp +
                " UniqueHeaderId=" + UniqueHeaderId +
                " eventId=" + this.eventId +
                " textKey=" + this.textKey +
                " data=" + this.data +
                " ComponentId=" + ComponentId;

            return userEventString;
        }
    }
}
